﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.Koc;
using System.Windows.Threading;
using System.Collections.ObjectModel;

namespace CSharp.Libs.WPF.UserControls
{
    public class KocSeedViewerControlViewModel : ViewModelBase
    {
        #region declarations
        private Dispatcher _DISP = null;
        private Kernel _KRNL = null;
        #endregion



        #region constructor / finalizer
        public KocSeedViewerControlViewModel(
            Dispatcher DISP,
            Kernel KRNL)
        {
            //set references
            _DISP = DISP;
            _KRNL = KRNL;

            //event subscriptions
            _KRNL.SC.SeedReceived += new SeedCollector.SeedReceivedEventHandler(SC_SeedReceived);
        }

        ~KocSeedViewerControlViewModel()
        {
            //unsubscibe from events
            _KRNL.SC.SeedReceived -= SC_SeedReceived;

            //Only release following references. Disposing has to happen in another place.
            _DISP = null;
            _KRNL = null;
        }
        #endregion



        #region event handling
        void SC_SeedReceived(object sender, Koc.KernelModels.SeedReceivedEventArgs srea)
        {
            //ObservableCollection<KocSeedViewerItem> oc = new ObservableCollection<KocSeedViewerItem>();

            //foreach (KeyValuePair<string, object> kvp in _KRNL.SC.Seeds)
            //{
            //    oc.Add(new KocSeedViewerItem
            //    {
            //        Key = kvp.Key,
            //    });
            //}

            //Seeds = oc;


            //KocSeedViewerItem ksvi = new KocSeedViewerItem();
            //ksvi.Key = srea.DictionaryKey;
            //ksvi.SeedText = srea.SeedText;


            _DISP.Invoke((Action)delegate
            {
                Seeds.Add(new KocSeedViewerItem { Key = srea.DictionaryKey, SeedText = srea.SeedText });
                //Seeds.Add(ksvi);
                OnPropertyChanged("Seeds");
            });
        }
        #endregion



        #region private functions
        #endregion



        #region interface
        #region properties
        private ObservableCollection<KocSeedViewerItem> _ocSeeds = new ObservableCollection<KocSeedViewerItem>();
        public ObservableCollection<KocSeedViewerItem> Seeds { get { return _ocSeeds; } set { _ocSeeds = value; OnPropertyChanged("Seeds"); } }
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
